//
//  ListDetailViewController.swift
//  Checklists
//
//  Created by cygarm on 15/11/30.
//  Copyright © 2015年 Razeware. All rights reserved.
//

import UIKit

protocol ListDetailViewControllerDelegate: class {
	func listDetailViewControllerDidCancel(controller: ListDetailViewController)
	func listDetailViewController(controller: ListDetailViewController, didFinishAddingChecklist checklist: Checklist)
	func listDetailViewController(controller: ListDetailViewController, didFinishEditingChecklist checklist: Checklist)
}

class ListDetailViewController: UITableViewController, UITextFieldDelegate, IconPickerViewControllerDelegete {

	@IBOutlet weak var textField: UITextField!
	@IBOutlet weak var doneBarButton: UIBarButtonItem!
	@IBOutlet weak var iconImageView: UIImageView!
	
	weak var delegate: ListDetailViewControllerDelegate?
	var checklistToEdit: Checklist?
	var iconName = "Folder"
	
	override func viewDidLoad() 
	{
		super.viewDidLoad()
		tableView.rowHeight = 44
		
		if let checklist = checklistToEdit {
			title = "Edit Checklist"
			textField.text = checklist.name
			doneBarButton.enabled = true
			iconName = checklist.iconName
		}
		
		iconImageView.image = UIImage(named: iconName)
	}
	
	override func viewWillAppear(animated: Bool) {
		super.viewWillAppear(animated)
		textField.becomeFirstResponder()
	}
	
	override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
		if segue.identifier == "PickIcon" {
			let controller = segue.destinationViewController as! IconPickerViewController
			controller.delegate = self
		}
	}
	
	@IBAction func cancel(sender: AnyObject) 
	{
		delegate?.listDetailViewControllerDidCancel(self)
	}
	
	@IBAction func done(sender: AnyObject) 
	{
		if let checklist = checklistToEdit {
			checklist.name = textField.text!
			checklist.iconName = iconName
			delegate?.listDetailViewController(self, didFinishEditingChecklist: checklist)
		} else {
			let checklist = Checklist(name: textField.text!, iconName: iconName)
			delegate?.listDetailViewController(self, didFinishAddingChecklist: checklist)
		}
	}
	
	//MARK: UITextFieldDelegate
	func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
		let oldText: NSString = textField.text!
		let newText: NSString = oldText.stringByReplacingCharactersInRange(range, withString: string)
		
		doneBarButton.enabled = (newText.length > 0)
		return true
	}
	
	//MARK: UITableViewDelegate
	override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
		if indexPath.section == 1 {
			return indexPath
		} else {
			return nil
		}
	}	
	
	//MARK: IconPickerViewControllerDelegate
	func iconPicker(picker: IconPickerViewController, didPickIcon iconName: String) {
		self.iconName = iconName
		iconImageView.image = UIImage(named: iconName)
		/*
		the segue style is "show" instead of "persent modally",which pushes the new view controller on the navigation stack.To return you need to "pop" it off again.
		so,don't call dismissViewController() here,because the Icon Picker is on the navigation stack
		*/
		navigationController?.popViewControllerAnimated(true)
	}
	
}
